/**
 * Created by MDURAND on 16/10/2014.
 *
 * Création du service qui donne accès au service d'authentification
 */
helloseriesApp.factory('AuthService', ['$http','$log', 'md5', 'ENV', 'UserModel',
    function ($http , $log, md5, ENV, UserModel ) {
        var authService = {};
        var currentUser = new UserModel();

        authService.login = function (credentials) {
            $log.log("Logging...");
            return $http
                .get( ENV.helloseriesEndPoint + 'login', {
                    params : {
                        username : credentials.username.toLowerCase(),
                        password : md5.createHash(credentials.password)
                    }
                })
                .success(function (retour) {
                    $log.log("Logging success")
                    return retour;
                })
                .error(function (error){
                    $log.log("Logging fails " +error);
                });
        };

        authService.setCurrentUser = function (user){
            currentUser = user;
        };

        authService.getCurrentUser = function (){
           return currentUser;
        };


        authService.isAuthenticated = function () {
            return currentUser !=  {};
        };


        return authService;
    }
]);
